#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/10/25 17:13
# @Author  : supuchun
# @Site    : 
# @File    : write.py


import os,requests
import pymysql
def open_db():
    global db, cursor
    db = pymysql.connect("rm-2ze108d8naeso02i1o.mysql.rds.aliyuncs.com", "elephant", "K6hd562P1m", "compare_v2",charset='utf8')
    cursor = db.cursor()
def close_db():
    global db
    db.close()

def write_data(data):
    with open ('1.csv','a',charset='utf-8') as f:
        f.write(data+"\n")


def get_data():
    global db, cursor
    sql = """select company_id,id,oss_url from compare_100004 where size > 30 and width/height >= 0.3062 and width/height <= 3.26 and img_format < 3 """
    try:
        # 执行sql语句
        cursor.execute(sql)
        # 提交到数据库执行
        db.commit()
        result = cursor.fetchAll()
    except:
        # 如果发生错误则回滚
        #print(sql)
        db.rollback()
        return False
    return result


open_db()
data = get_data()

for i in data:
    oss_url = "http://elephant-spider.vpc100-oss-cn-beijing.aliyuncs.com/"+i['oss_url']+"?x-oss-process=image/resize,m_lfit,h_400,w_400"
    url = "http://172.16.237.71/getfeatures?ossurl="+oss_url
    feature = requests.get(url)
    str = "100004-"+i['id']+"   "+feature
    write_data(str)